﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDService.SocketHandle
// 文 件 名：	PH05ReGotTransInfo
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2017/9/27 14:46:31
//========================================
using Common;
using Network.UpdateData;
using System.Net.Sockets;

namespace SFJDService.SocketHandle
{
    /// <summary>
    /// TCP， code: 1304
    /// (Recive端)接收到文件列表，当接收到的内容为空时，在接收列表里删除当前任务，然后广播发送1305指令
    /// </summary>
    class PH05ReGotTransInfo : PacketHandle
    {
        public PH05ReGotTransInfo(Socket socket, Packet packet) : base(socket, packet)
        {
        }

        protected override void Handle()
        {
            string[] buffe = Message.Split('♠');
            string guid = buffe[0];
            //Log.ToConsole("            " + (buffe[1] == ""));
            if (buffe[1] != "")
            {
                //string str = message.Substring(37, message.Length - 37);
                //Log.ToConsole(SFJDService.transport.Count.ToString() + "     " + SFJDService.transport[0] + "+++++++++");
                for (int i = 0; i < Runtime.transportReceive.Count; i++)
                {
                    string[] ss = Runtime.transportReceive[i].Split('♠');
                    if (ss[1].Equals(guid))
                    {
                        Runtime.transportReceive[i] += buffe[1];
                        break;
                    }
                }
            }
            else
            {
                Delay(100);
                bool isThereTransTask = false;
                for (int i = 0; i < Runtime.transportReceive.Count; i++)
                {
                    string[] ss = Runtime.transportReceive[i].Split('♠');
                    if (ss[1].Equals(guid))
                    {
                        ClassTransportInfo transInfo = new ClassTransportInfo();
                        //transInfo.DecodingFromString(ss[2]);
                        transInfo.DecodingFromString(ss[2]);

                        //new XmlTransport(TransportType.Download, Runtime.pathTemp).AddTransport(transInfo);
                        Runtime.xmlDownload.AddTransport(transInfo);
                        //获取10个端口号
                        int[] ports = GetAvailablePort.GetAvailablePorts(10);
                        string s = "";
                        foreach(int port in ports)
                        {
                            s = s + port + "♥";
                        }
                        string sport = s.Substring(0, s.Length - 1);

                        //删除缓存中当前GUID的列表
                        Runtime.transportReceive.RemoveAt(i);
                        Launcher.SendMsg2One(typeof(PH05SeStratSend).GetDictianaryKey(), guidSend, guid + '♠' + Runtime.thisUser.UserName + '♠' + sport);
                        isThereTransTask = true;
                    }
                }
                if (!isThereTransTask)
                {
                    throw new System.Exception();
                }
            }
        }
    }
}
